package com.yc.snacknet_vue.controller;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.snacknet_vue.bean.MemberInfo;
import com.yc.snacknet_vue.biz.IMemberInfoBiz;
import com.yc.snacknet_vue.biz.Impl.MemberInfoBizImpl;
import com.yc.snacknet_vue.utils.RequestToObject;
import com.yc.snacknet_vue.utils.SessionKeys;
import com.yc.snacknet_vue.utils.StringUtil;

/**
 * 会员信息业务控制层
 * @author UNO
 * @date 2021年8月6日
 * Email 2759451734@qq.com
 */
@WebServlet("/mem/*")
public class MemberInfoController extends BaseServlet{

	private static final long serialVersionUID = 1L;

	//注册
	protected void reg(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String nickName = request.getParameter("nickName");
		String pwd = request.getParameter("pwd");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		if(StringUtil.checkNull(nickName, pwd, tel, email)) {
			this.send(response, 501, null);
			return;
		}
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		int result = biz.reg(nickName, pwd, email, tel);
		if(result < 0) {
			this.send(response, 500,null);
		}
		this.send(response, 200, result);
	}
	//登录
	protected void login(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//想办法把请求的参数封装成对象饭返回 ？
		MemberInfo mem = RequestToObject.getParamesToObject(MemberInfo.class, request);
		System.out.println( "mem中的数据 : " + mem );
		
		if( null == mem ) {
			this.send(response, 501, null); //未从前台取到数据  或登录信息不完整
			return;
		}
		//新的登录方式不用验证码
		//  获取系统的验证 验证码验证
//		Object obj = request.getSession().getAttribute("vcode");
//		//判断
//		if( null == obj) {
//			this.send(response, 502, null);//系统验证码存储失败
//			return;
//		}
//		//验证码匹配
//		if( !String.valueOf(obj).equalsIgnoreCase(mem.getRealName())) {
//			this.send(response, 503, null);//验证码匹配日失败
//			return;
//		}
		//登录业务
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		MemberInfo member = biz.login(mem);
		//判断
		if( null == member) {
			this.send(response, 500, null);//登录失败 账号或密码错误
			return;
		}
		//登录成功
		request.getSession().setAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT, member);
		//响应数据
		this.send(response, 200, null);
	}
	
	//检查前台登录
	protected void check(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//获取session中的用户u信息
		Object obj = request.getSession().getAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT);
		if( null == obj) { //说明未登录
			this.send(response, 500, null);//验证码匹配日失败
			return;
		}
		this.send(response, 200, obj);
	}
	
	//后台会员查询 findByFirst
	protected void findByPageFirst(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int page= Integer.parseInt(request.getParameter("page")); //每页页码数
		int rows= Integer.parseInt(request.getParameter("limit")); //每页页码数
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		this.send(response, biz.totals(null), biz.finds(null, page, rows));
		
	}
	
	//修改重置密码
	protected void resetPwd(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String mno = request.getParameter("mno");
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		this.send(response, biz.update(mno));
	}
	
	//修改用户状态
	protected void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String mno = request.getParameter("mno");
		String status = request.getParameter("status");
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		this.send(response, biz.delete(status, mno));
	}
	
	//分页查询
	protected void findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));  //当前页码数
		int rows = Integer.parseInt(request.getParameter("limit"));  //每页显示数
		String mno = request.getParameter("mno");
		String tel = request.getParameter("tel");
		String nickName = request.getParameter("nickName");
		String status = request.getParameter("status");
		
		MemberInfo mem = new MemberInfo();
		if(!StringUtil.checkNull(mno)) {
			mem.setMno(Integer.parseInt(mno));
		}
		if(!StringUtil.checkNull(status)) {
			mem.setMno(Integer.parseInt(status));
		}
		mem.setTel(tel);
		mem.setNickName(nickName);
		
		IMemberInfoBiz biz = new MemberInfoBizImpl();
		this.send(response, biz.totals(mem), biz.finds(mem, page, rows));
				
		
	}
	
	
}
